package cn.iocoder.yudao.module.psy.controller.admin.resource;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.psy.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.psy.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.module.psy.service.resource.ResourceService;

@Tag(name = "管理后台 - 心理评估资源")
@RestController
@RequestMapping("/psy/resource")
@Validated
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建心理评估资源")
    @PreAuthorize("@ss.hasPermission('psy:resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody ResourceSaveReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新心理评估资源")
    @PreAuthorize("@ss.hasPermission('psy:resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody ResourceSaveReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除心理评估资源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('psy:resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得心理评估资源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('psy:resource:query')")
    public CommonResult<ResourceRespVO> getResource(@RequestParam("id") Long id) {
        ResourceDO resource = resourceService.getResource(id);
        return success(BeanUtils.toBean(resource, ResourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得心理评估资源分页")
    @PreAuthorize("@ss.hasPermission('psy:resource:query')")
    public CommonResult<PageResult<ResourceRespVO>> getResourcePage(@Valid ResourcePageReqVO pageReqVO) {
        PageResult<ResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResourceRespVO.class));
    }

    @GetMapping("/random")
    @Operation(summary = "获得心理评估资源分页")
    @PreAuthorize("@ss.hasPermission('psy:resource:query')")
    public CommonResult<PageResult<ResourceRespVO>> getResourceRandowm(@Valid  Integer size) {
        ResourcePageReqVO pageReqVO =new ResourcePageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setType(2);
        PageResult<ResourceDO> pageResult = resourceService.getResourcePage(pageReqVO);
        List<ResourceDO>  list=pageResult.getList();
        if (list == null || list.isEmpty()) {
           return CommonResult.error(500,"数据错误");
        }
        // 使用 Random 类生成随机数量
        Random random = new Random();
        int count = random.nextInt(list.size()) + 1; // 随机数量范围为 [1, list.size()]
        // 复制原列表以避免修改原始数据
        List<ResourceDO> tempList = new ArrayList<>(list);

        // 打乱列表顺序
        Collections.shuffle(tempList);
         pageResult.setList( tempList.subList(0, size));
        return success(BeanUtils.toBean(pageResult, ResourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出心理评估资源 Excel")
    @PreAuthorize("@ss.hasPermission('psy:resource:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResourceExcel(@Valid ResourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceDO> list = resourceService.getResourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "心理评估资源.xls", "数据", ResourceRespVO.class,
                        BeanUtils.toBean(list, ResourceRespVO.class));
    }

}